package com.framework.plm.bas.web.controller;

/**
 * Formated object for generating a json response to be used in a
 * net.autodata.JsonRequest
 */
public class JsonResponse {
	private Object result;
	private JsonError error;

	public JsonResponse() {
		this.setResult(null);
		this.error = null;
	}

	/**
	 * Creates a json response with a populated "error" node.
	 * 
	 * @param error
	 *            The json result object for a failed response.
	 */
	public JsonResponse(JsonError error) {
		this.setResult(null);
		this.error = error;
	}

	/**
	 * Creates a json response with a populated "result" node.
	 * 
	 * @param data
	 *            The successful data object that is being returned
	 */
	public JsonResponse(Object data) {
		this.setResult(data);
		this.error = null;
	}

	/**
	 * Creates a json response with a populated "error" node.
	 * 
	 * @param message
	 *            The error message contained in the response
	 * @param data
	 *            The failed data object that is being returned
	 */
	public JsonResponse(String message, Object data) {
		this.setResult(null);
		this.error = new JsonError(message, data);
	}

	/**
	 * Creates a json response with a populated "error" node.
	 * 
	 * @param number
	 *            The custom error number
	 * @param message
	 *            The error message contained in the response
	 * @param data
	 *            The failed data object that is being returned
	 */
	public JsonResponse(int number, String message, Object data) {
		this.setResult(null);
		this.error = new JsonError(number, message, data);
	}

	/**
	 * Creates a json response with a populated "error" node.
	 * 
	 * @param number
	 *            The custom error number
	 * @param message
	 *            The error message contained in the response
	 * @param type
	 *            The error type (ErrorType)
	 * @param data
	 *            The failed data object that is being returned
	 */
	public JsonResponse(int number, String message, String type, Object data) {
		this.setResult(null);
		this.error = new JsonError(number, message, type, data);
	}

	public JsonError getError() {
		return error;
	}

	public void setError(JsonError error) {
		this.error = error;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public Object getResult() {
		return result;
	}

	public interface ErrorTypes {
		String OMS = "oms";
		String DECORATOR = "decorator";
	}

	/**
	 * Structured json error object, to be used when an error occurs in the
	 * system.
	 */
	public class JsonError {
		private int number = -1;
		private String message;
		private Object data;
		private String type = "oms";

		public JsonError() {
		}

		public JsonError(String message, Object data) {
			this.message = message;
			this.data = data;
		}

		public JsonError(int number, String message, Object data) {
			this.number = number;
			this.message = message;
			this.data = data;
		}

		public JsonError(int number, String message, String type, Object data) {
			this.number = number;
			this.message = message;
			this.data = data;
			this.type = type;
		}

		public int getNumber() {
			return number;
		}

		public void setNumber(int number) {
			this.number = number;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}
	}
}
